<?php
/**
 * 广告
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use app\library\Flag;
use think\Model;
use think\Db;

class Ad extends Model
{
    //set talbename
    protected $table = 'ad';
    //set primary key
    protected $pk = 'ad_id';
    protected $autoWriteTimestamp = true;

    /**
     * @desc 添加广告
     * @param $param
     * @return false|int
     */
    public function addAd($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    public function updateAd($ad_id, $param)
    {
        $conds = array(
            'ad_id' => $ad_id,
        );
        $field = array();
        if (isset($param['ad_name'])) {
            $field['ad_name'] = $param['ad_name'];
        }

        if (isset($param['ad_link'])) {
            $field['ad_link'] = $param['ad_link'];
        }

        if (isset($param['ad_code'])) {
            $field['ad_code'] = $param['ad_code'];
        }

        if (isset($param['start_time'])) {
            $field['start_time'] = $param['start_time'];
        }

        if (isset($param['end_time'])) {
            $field['end_time'] = $param['end_time'];
        }

        if (isset($param['enabled'])) {
            $field['enabled'] = $param['enabled'];
        }

        if (isset($param['orderby'])) {
            $field['orderby'] = $param['orderby'];
        }

        if (isset($param['status'])) {
            $field['status'] = $param['status'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @desc 根据条件获取广告
     * @param $conds $fields
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAdByCond($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            //    if (in_array($key, $this->field)) {
            $arrCond[$key] = $val;
            //    }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            //    if (in_array($val, $this->field)) {
            $arrField[] = $val;
            //    }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * @desc 获取广告
     * @param $param
     * @return false|int
     */
    public function getAd()
    {
        return Db::table($this->table)->field(true)->where(['status' => '0'])->select();
    }

    /**
     * @return \think\paginator\Collection
     */
    public function getAdPage()
    {
        return $this->where(['status' => '0'])->paginate(10);
    }

    public function getAdByPid($pid)
    {
        $conds = array(
            'pid' => $pid,
        );
        return Db::table($this->table)->where($conds)->order('orderby desc')->select();
    }
}
